package com.von.handler;

import com.baomidou.mybatisplus.extension.api.R;
import com.von.enums.ErrorEnum;
import com.von.exceptions.BusinessRuntimeException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author pengzhaofeng
 * @date 2020-05-26 20:41
 */
@ControllerAdvice
public class GloablExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        e.printStackTrace();
        String msg = e.getMessage();
        if (msg == null || msg.equals("")) {
            e.printStackTrace();
            msg = "系统繁忙";
        }
        return R.failed(msg);
    }

    @ResponseBody
    @ExceptionHandler(BusinessRuntimeException.class)
    public Object handleException(BusinessRuntimeException e) {
        ErrorEnum errorEnum = e.getErrorEnum();
        return R.failed(errorEnum.getError()).setCode(errorEnum.getCode());
    }
}
